/*
 * @Date: 2024-11-11 11:23:51
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2024-11-11 13:59:11
 * @Description: 完善项目申报操作方法
 */
import XEUtils from 'xe-utils';
import HandlerUtils from '@/handler/utils/HandlerUtils.js'

import { reactive } from "vue"
import { useProjectApi } from '@/api/index'

import { RkConfirmBox } from '@/components/message-box/index.js'

import { openOperationLogs } from '@/plugins/call-comp/CallCompService.js'
import { callComp, openXmsbDialog } from '@hd/base/service/HdCallCompService.js'

import ProjTableDialog from '@/handler/dialogs/ProjTableDialog.vue'

import WfTask from '@/handler/pojo/WfTask.js'

const useLayoutHandler = (params) => {
  // 参数
  const { routeQuery, userInfo, tableSearch } = params

  // 基本信息
  const baseData = reactive({
    // 选中项目ID
    proId: '',
    // 选中区划编码
    adCode: userInfo.adBjCode,
    // 单位ID
    agId: '',
    // 选中单位编码
    agCode: '',
    // 单位名称
    agName: '',
    // 上级单位Id
    agParentId: '',
    // 工作流
    processKey: routeQuery.processKey,
  })
  /**
   * 表单渲染业务按钮点击事件
   */
  const btnGroupRightClick = async ({ rightItem, extParams }) => {
    const { agData, checkRows } = extParams
    const btnType = rightItem.value
    if (btnType === 'add') {
      const addInfo = {
        ...XEUtils.clone(baseData, true),
        agCode: agData.code,
      }
      handleOpenXzxmDialog('add', addInfo)
    } else if (btnType === 'edit') {
      const row = checkRows[0]
      const editInfo = {
        ...XEUtils.clone(baseData, true),
        ...baseData,
        agId: row.agId,
        proId: row.proId,
        agName: row.agName,
        agCode: row.agCode,
        applyId: row.applyId,
        perfectId: row.perfectId,
      }
      handleOpenXmsbDialog('edit', editInfo)
    } else if (btnType === 'send') {
      HandlerUtils.openLoadService(rightItem.label)
      const reqParams = coverOperateParams(checkRows, WfTask.SEND.value)
      const result = await useProjectApi().improveProHandleTask(reqParams)
      const { data, code, msg } = result
      if (code === 200) {
        HandlerUtils.successMsgTip(rightItem.label)
        tableSearch()
      } else {
        HandlerUtils.failedMsgTip(result.msg, true)
      }
      HandlerUtils.closeLoadService()
    } else if (btnType === 'cancel') {
      RkConfirmBox({
        title: '提示',
        content: `您确定要${rightItem.label}吗？`,
        onSubmit: async () => {
          HandlerUtils.openLoadService(rightItem.label)
          const reqParams = coverOperateParams(checkRows, WfTask.CANCEL.value)
          const result = await useProjectApi().improveProHandleTask(reqParams)
          if (result.code === 200) {
            HandlerUtils.successMsgTip(rightItem.label)
            tableSearch()
          } else {
            HandlerUtils.failedMsgTip(result.msg, true)
          }
          HandlerUtils.closeLoadService()
        }
      })
    } else if (btnType === 'delete') {
      RkConfirmBox({
        title: '提示',
        content: `您确定要${rightItem.label}吗？`,
        onSubmit: async function () {
          HandlerUtils.openLoadService(rightItem.label)
          const reqParams = coverOperateParams(checkRows, WfTask.CANCEL.value)
          const result = await useProjectApi().deleteProResPerfect({
            perfectIds: reqParams.perfectIds
          })
          if (result.code === 200) {
            HandlerUtils.successMsgTip(rightItem.label)
            tableSearch()
          } else {
            HandlerUtils.failedMsgTip(result.msg, true)
          }
          HandlerUtils.closeLoadService()
        }
      })
    } else if (btnType === 'log') {
      openOperationLogs(checkRows)
    }
  }
  /**
   * 打开选择项目弹窗
   * 
   * @param type 类型
   * @param info 信息
   */
  const handleOpenXzxmDialog = (type, info) => {
    callComp.open(ProjTableDialog, {
      type: type,
      extParams: { ...info },
      className: 'rk-modal proj-table-dialog',
      onConfirm: (row) => {
        console.log('row', row)
        const addInfo = {
          ...baseData,
          agId: row.agId,
          agName: row.agName,
          agCode: row.agCode,
          proId: row.proId,
          applyId: row.applyId,
          perfectId: row.perfectId,
        }
        handleOpenXmsbDialog('add', addInfo)
      }
    })
  }

  /**
   * 打开完善项目申报弹窗
   * 
   * @param {String} type 类型
   * @param {Object} info 信息
   */
  const handleOpenXmsbDialog = (type, info) => {
    openXmsbDialog({
      type: type,
      title: '完善项目申报',
      formCode: routeQuery.tbFormCodeA,
      compType: 'improve',
      extParams: {
        ...info,
        formType: 'improve',
        formCodeA: routeQuery.tbFormCodeA,
        formCodeB: routeQuery.tbFormCodeB,
      },
      onConfirm: () => {
        tableSearch()
      }
    })
  }

  /**
   * 封装操作数据
   * 
   * @param checkRows 选中的数据
   * @param wfType 工作流状态
   */
  const coverOperateParams = (checkRows, wfType) => {
    let proIds = []
    let taskIds = []
    let applyIds = []
    let perfectIds = []
    for (const row of checkRows) {
      proIds.push(row.proId)
      taskIds.push(row.taskId)
      applyIds.push(row.applyId)
      perfectIds.push(row.perfectId)
    }
    return {
      // 流程节点
      nodeId: routeQuery.nodeId,
      proIds: proIds.join(','),
      taskIds: taskIds.join(','),
      applyIds: applyIds.join(','),
      perfectIds: perfectIds.join(','),
      taskType: wfType || '',
      adCode: baseData.adCode,
      agCode: '',
      processKey: baseData.processKey,
      bondTypeCode: 2,
    }
  }

  return {
    baseData,
    buttonClick: null,
    btnGroupRightClick
  }
}

export default useLayoutHandler
